package com.texaco.chapasFutbol.buttons;

import java.util.ArrayList;
import java.util.List;

import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.AutoWrap;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.util.HorizontalAlign;

import android.content.res.Resources;

import com.texaco.chapasFutbol.ChapasFutbolActivity;
import com.texaco.chapasFutbol.entidades.Game;

/**
 * Clase que implementa los botones que vamos a usar en nuestra app.
 * @author texaco
 *
 */
public class Button {
	
	/**
	 * Indica el numero máximo de caracteres que se puede mostrar en un botón.
	 */
	private static final int MAX_CHARACTERS = 20;
	
	private ChapasFutbolActivity activity;
	private Sprite sprite;
	private List<Text> text;
	private Scene scene;
	
//	public Button(Game game, 
//			Scene scene, 
//			float x, 
//			float y, 
//			List<Integer> textId, 
//			List<Font> font, 
//			String shape, 
//			final IButtonCallBack callBack){
//		super();
//		this.activity = game.getActivity();
//		this.scene = scene;
//		Resources resources = this.activity.getResources();
//		
//		List<String> textos = new ArrayList<String>();
//		for(int i = 0; i < textId.size(); i++){
//			textos.add((String) resources.getText(textId.get(i)));
//		}
//		init(game, scene, x, y, textos, font, shape, callBack);
//	}
//	
	public Button(Game game, 
			Scene scene, 
			float x, 
			float y, 
			List<String> textStr, 
			List<Font> font, 
			String shape, 
			final IButtonCallBack callBack){
		super();
		this.activity = game.getActivity();
		this.scene = scene;
		init(game, scene, x, y, textStr, font, shape, callBack);
	}	
	
	public Button(Game game, 
			Scene scene,
			IEntity parent,
			float x, 
			float y, 
			List<String> textStr, 
			List<Font> font, 
			String shape, 
			final IButtonCallBack callBack){
		super();
		this.activity = game.getActivity();
		this.scene = scene;
		init(game, parent, x, y, textStr, font, shape, callBack);
	}
	
	public Button(Game game, 
			Scene scene, 
			float x, 
			float y, 
			String textStr, 
			Font font, 
			String shape, 
			final IButtonCallBack callBack){
		super();
		this.activity = game.getActivity();
		this.scene = scene;
		
		List<String> texts = null;
		List<Font> fonts = null;
		
		if(textStr != null && textStr.length() > 0){
			texts = new ArrayList<String>();
			fonts = new ArrayList<Font>();
			texts.add(textStr);
			fonts.add(font);
		}
		
		init(game, scene, x, y, texts, fonts, shape, callBack);
	}
	
	public Button(Game game, 
			Scene scene, 
			float x, 
			float y, 
			int textId, 
			Font font, 
			String shape, 
			final IButtonCallBack callBack){
		super();
		this.activity = game.getActivity();
		this.scene = scene;
		
		List<String> texts = new ArrayList<String>();
		List<Font> fonts = new ArrayList<Font>();
		texts.add((String) this.activity.getResources().getText(textId));
		fonts.add(font);
		init(game, scene, x, y, texts, fonts, shape, callBack);
	}
	
	public Button(Game game, 
			Scene scene,
			IEntity parent,
			float x, 
			float y, 
			int textId, 
			Font font, 
			String shape, 
			final IButtonCallBack callBack){
		super();
		this.activity = game.getActivity();
		this.scene = scene;
		
		List<String> texts = new ArrayList<String>();
		List<Font> fonts = new ArrayList<Font>();
		texts.add((String) this.activity.getResources().getText(textId));
		fonts.add(font);
		init(game, parent, x, y, texts, fonts, shape, callBack);
	}
	
	private void init(Game game, IEntity parent, float x, float y,
			List<String> textos, List<Font> font, String shape,
			final IButtonCallBack callBack) {
		this.sprite = new Sprite(x, y, 
				this.activity.getShapesMaps().get(shape), 
				this.activity.getVertexBufferObjectManager()){
			
			@Override
			public boolean onAreaTouched(
					TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				boolean fired = false;
				if(pSceneTouchEvent.isActionUp()){
					if(callBack != null){
						callBack.fire();
						fired = true;
					}
				}
				return fired;
			}
		};

		this.text = null;
		
		/*
		 * Obtenemos el alto total de las fuentes.
		 */
		if(textos != null && !textos.isEmpty() && 
				font != null && !font.isEmpty()){
			float alto = 0;
			for(int i = 0; i < font.size(); i++){
				alto += font.get(i).getLineHeight();
			}
			
			/*
			 * Obtenemos la posicion inicial.
			 */
			float pos = (sprite.getHeight() - alto)/2;
			
			for(int i = 0; i < textos.size(); i++){
				if(textos.get(i) != null && textos.get(i).length() > 0){
					if(this.text == null){
						this.text = new ArrayList<Text>();
					}
					this.text.add(new Text(x, y + pos, font.get(i),
							textos.get(i), MAX_CHARACTERS,
							new TextOptions(AutoWrap.WORDS, sprite.getWidth(), HorizontalAlign.CENTER, 0),
							this.activity.getVertexBufferObjectManager()));
					pos += font.get(i).getLineHeight();
				}
			}
		}
		IEntity par = parent;
		if(par == null){
			par = this.scene;
		}
		
		par.attachChild(sprite);
		if(text != null && !text.isEmpty()){
			for(Text textTmp : text){
				par.attachChild(textTmp);
			}
		}
		this.scene.registerTouchArea(sprite);
	}
	
	/**
	 * Obtiene el texto del boton.
	 * @return
	 */
	public String getText(){
		if(text != null && !text.isEmpty()){
			StringBuffer str = new StringBuffer();
			for(Text textTemp : text){
				str.append(textTemp.getText());
			}
			return str.toString();
		}
		return null;
	}
	
	/**
	 * Establece el texto del boton.
	 * @param text
	 */
	public void setText(String text){
		if(this.text != null && !this.text.isEmpty()){
			this.text.get(0).setText(text);
		}
	}
	
	/**
	 * Elimina el botón.
	 */
	public void dispose(){
		this.detach();
		sprite.dispose();
		if(text != null && !text.isEmpty()){
			for(Text textTmp : text){
				textTmp.dispose();
			}
			text = null;
		}
	}

	/**
	 * Saca el boton de la escena
	 */
	public void detach() {
		scene.unregisterTouchArea(sprite);
		if(text != null && !text.isEmpty()){
			for(Text textTmp : text){
				scene.detachChild(textTmp);
			}
		}
		scene.detachChild(sprite);
	}
	
	/**
	 * Añade el boton a la escena
	 */
	public void attach(){
		if(!sprite.hasParent()){
			scene.attachChild(sprite);
			if(text != null && !text.isEmpty()){
				for(Text textTmp : text){
					scene.attachChild(textTmp);
				}
			}
			scene.registerTouchArea(sprite);
		}
	}

	/**
	 * Obtiene el ancho del botón.
	 * @return
	 */
	public float getWidth() {
		return sprite.getWidth();
	}
	
	/**
	 * Obtiene el alto del botón.
	 * @return
	 */
	public float getHeight(){
		return sprite.getHeight();
	}

	public void setPosition(float x, float y) {
		this.sprite.setPosition(x, y);
		if(this.text != null && !this.text.isEmpty()){
			float alto = 0;
			for(Text textTmp : this.text){
				alto += textTmp.getFont().getLineHeight();
			}
			float pos = (sprite.getHeight() - alto)/2f;
			for(Text textTmp : this.text){
				textTmp.setPosition(x, y + pos);
				pos += textTmp.getFont().getLineHeight();
			}
		}
	}
}